package com.zretc.auth.service;

import com.common.core.constant.SecurityConstants;
import com.common.core.constant.UserConstants;
import com.common.core.domain.CodeMessage;
import com.common.core.exception.ServiceException;
import com.common.core.utils.StringUtils;
import com.zretc.pojo.LoginUserPojo;
import com.zretc.api.service.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysLoginService {
    @Autowired
    private RemoteUserService remoteUserService;
    public LoginUserPojo login(String username, String password) {
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(username, password))
        {
            throw new ServiceException("用户/密码必须填写");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            throw new ServiceException("用户密码不在指定范围");
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            throw new ServiceException("用户名不在指定范围");
        }
        // 查询用户信息
        CodeMessage<LoginUserPojo> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
        if (userResult.getCode() == CodeMessage.FAIL) {
            throw new ServiceException("连接不到服务端mazheng-System");
        }

        return userResult.getData();
    }
}
